package com.wpre.booksystem.controller;

import com.wpre.booksystem.enums.BookStatusEnum;
import com.wpre.booksystem.model.BookInfo;
import com.wpre.booksystem.model.PageRequest;
import com.wpre.booksystem.model.PageResponse;
import com.wpre.booksystem.model.Result;
import com.wpre.booksystem.service.BookService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-10-25
 * Time: 16:32
 */
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    //添加图书
    @RequestMapping(value = "/addBook", produces = "application/json")//返回类型改成json
    public String addBook(BookInfo bookInfo) {
        //1.参数校验

        if (bookInfo == null) {
            return "bookInfo为空";
        }
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null) {
            return "参数不合法";
        }

        //2.插入数据
        //插入的时候可能抛出异常
        try {
            Integer result = bookService.insertBook(bookInfo);
            if (result == 1) {
                return "";
            }
        } catch (Exception e) {
            log.error("插入失败,e", e);
        }

        return "插入失败";
    }

    //查询图书信息(翻页功能)
    @RequestMapping("/getListByPage")
    public Result<PageResponse<BookInfo>> getListByPage(PageRequest pageRequest, HttpServletRequest request) {
        log.info("获取图书列表:", pageRequest);
        //参数校验
        if (pageRequest == null || pageRequest.getCurrentPage() < 0 || pageRequest.getPageSize() == null) {
            return Result.fail();
        }
        //调用Service层
        PageResponse<BookInfo> pageResponse = bookService.getListByPage(pageRequest);
        return Result.success(pageResponse);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        log.info("查询图书,bookId:", bookId);
        return bookService.queryBookById(bookId);
    }

    //更新图书
    @RequestMapping(value = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        try {
            Integer result = bookService.updateBook(bookInfo);
            log.info("result:{}", result);
            if (result > 0) {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "图书更新失败";

    }

    //删除图书
    @RequestMapping(value = "/deleteBook", produces = "application/json")
    public String deleteBook(Integer id) {
        try {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(id);
            bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
            Integer result = bookService.updateBook(bookInfo);
            if (result > 0) {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "图书删除失败";

    }

    //批量删除
    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids) {

        try {
            bookService.batchDelete(ids);
            return true;
        } catch (Exception e) {
            log.error("批量删除失败", e);
        }

        return false;
    }

}
